import 'package:flutter/material.dart';
import 'dart:async';

enum Action {
  Cancel, Ok
}

class AlertDialogDemo extends StatefulWidget {
  AlertDialogDemo({Key key}) : super(key: key);

  @override
  _AlertDialogDemoState createState() => _AlertDialogDemoState();
}

class _AlertDialogDemoState extends State<AlertDialogDemo> {
  String _choise = 'nothing';
  Future _openAlertDialog() async {
    Action action = await showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('AlertDialogDemo'),
          content: Text('Are you sure about this?'),
          actions: [
            FlatButton(
              onPressed: ()=>Navigator.pop(context, Action.Cancel), 
              child: Text('cancel')
            ),
            FlatButton(
              onPressed: ()=>Navigator.pop(context, Action.Ok), 
              child: Text('sure')
            )
          ],
        );
      }
    );

    switch (action) {
      case Action.Cancel:
        setState(() {
          _choise = 'cancel';
        });
        break;
      case Action.Ok:
        setState(() {
          _choise = 'ok';
        });
        break;
      default:
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AlertDialogDemo'),
        elevation: 0.0,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('your choise is: $_choise'),
          SizedBox(height:16),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                child: Text('Open AlertDialog'),
                onPressed: _openAlertDialog,
              )
            ],
          )
        ],
      ),
    );
  }
}